package jacob.id.ants;

import java.util.ArrayList;

import jacob.id.ants.GraphicObject;
import jacob.id.ants.GraphicObject.Coordinates;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.ImageView;
import jacob.id.ants.GameThread;
import jacob.id.ants.AntProjectActivity;



public class Panel extends SurfaceView implements SurfaceHolder.Callback  {
	private GameThread mThread;
	private ArrayList<GraphicObject> mGraphics;
	private GraphicObject mAnt;
	private ArrayList<GraphicObject> mMonster;
	private ArrayList<GraphicObject> mApples;
	int numMonsters = 2;
	boolean spawn = false;
	int numApples = 5;
	int lvlNum;
	int numApplesCol = 5;
	public boolean win = false;
	public boolean lose = false;
	int numDied;
	int numLives = 3;
	private AntProjectActivity hello;
	


	public Panel(AntProjectActivity context) {
		super(context);
		getHolder().addCallback(this);
		setFocusable(true);
		mThread = new GameThread(getHolder(), this);
		hello = context;

		Bundle bundle = new Bundle();
		boolean b = bundle.getBoolean("NewGame");
		if(b == true){
			numLives = 3;
			numDied = 0;
			win = false;
			lose = false;
			lvlNum = 0;
			numApples = 0;
			numApplesCol = 0;
			createNewGame();

		}
	}



	@Override
	public void onDraw(Canvas canvas){
		canvas.drawColor(Color.BLACK);
		Bitmap bitmap;
		Coordinates coords;
		for (GraphicObject go: mGraphics) {
			bitmap = go.getBitmap();
			coords = go.getCoordinates();
			canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		}

	}
	
	public void updatePhysics(){
		//Will draw everything and make the game work will build after the class for ant and monsters are built	
		if(numApples == numApplesCol){
			win = true;
			Intent win_lose = new Intent(getContext(), win.class);
			Bundle winDetails = new Bundle();
			winDetails.putBoolean("win_lose", true);
			win_lose.putExtra("win_lose", winDetails);
			hello.startActivity(win_lose);

		}
		
		for(int i = 0; i < mMonster.size(); i++){
			int tLX= mMonster.get(i).getCoordinates().getX();
			int tLY = mMonster.get(i).getCoordinates().getY();
			Rect mon1 = new Rect(tLX, tLY, mMonster.get(i).getCoordinates().getX() - mMonster.get(i).getBitmap().getWidth(), mMonster.get(i).getCoordinates().getY() - mMonster.get(i).getBitmap().getHeight());
			Rect antRect = new Rect(mAnt.getCoordinates().getX(), mAnt.getCoordinates().getY(), mAnt.getCoordinates().getX() - mAnt.getBitmap().getWidth(), mAnt.getCoordinates().getY() - mAnt.getBitmap().getHeight());
			if(mon1.intersect(antRect)){
				createNewGame();
				numDied++;
			}
		}
		
		for(int i = 0; i < mApples.size(); i++){
			Rect antRect = new Rect(mAnt.getCoordinates().getX(), mAnt.getCoordinates().getY(), mAnt.getCoordinates().getX() - mAnt.getBitmap().getWidth(), mAnt.getCoordinates().getY() - mAnt.getBitmap().getHeight());
			Rect appleRect = new Rect(mApples.get(i).getCoordinates().getX(), mApples.get(i).getCoordinates().getY(), mApples.get(i).getCoordinates().getX() + mApples.get(i).getBitmap().getWidth(), mApples.get(i).getCoordinates().getY() - mApples.get(i).getBitmap().getHeight());
			if(antRect.intersect(appleRect)){
					
			}
		}

		for(int i = 0; i < numMonsters; i ++){
			int monX = mMonster.get(i).getCoordinates().getX();
			int monY = mMonster.get(i).getCoordinates().getY();

			if(monX >= getWidth()){
				mMonster.get(i).getCoordinates().setX(getWidth());
			}
			if(monX <= 0){
				mMonster.get(i).getCoordinates().setX(0);
			}
			if(monY >= getHeight()){
				mMonster.get(i).getCoordinates().setY(getHeight());
			}
			if(monY <= 0){
				mMonster.get(i).getCoordinates().setY(0);
			}	
		}
		if(mAnt.getCoordinates().getX() + mAnt.getBitmap().getWidth() >= getWidth()){
			mAnt.getCoordinates().setX(getWidth() - mAnt.getBitmap().getWidth());
		}
		if(mAnt.getCoordinates().getX() <= 0){
			mAnt.getCoordinates().setX(0);
		}
		if(mAnt.getCoordinates().getY() + mAnt.getBitmap().getHeight() >= getHeight()){
			mAnt.getCoordinates().setY(getHeight() - mAnt.getBitmap().getHeight());
		}
		if(mAnt.getCoordinates().getY()  <= 0){
			mAnt.getCoordinates().setY(0);
		}	


		for(int i = 0; i < mMonster.size(); i++){
			int monX = mMonster.get(i).getCoordinates().getX();
			int monY = mMonster.get(i).getCoordinates().getY();
			if(monX < mAnt.getCoordinates().getX()){
				mMonster.get(i).getCoordinates().setX(monX + 3);
			}
			if(monX > mAnt.getCoordinates().getX()){
				mMonster.get(i).getCoordinates().setX(monX - 3);
			}
			if(monY < mAnt.getCoordinates().getY()){
				mMonster.get(i).getCoordinates().setY(monY + 3);
			}
			if(monY > mAnt.getCoordinates().getY()){
				mMonster.get(i).getCoordinates().setY(monY - 3);
			}
		}
	}


	public void createNewGame(){
		//Create Monsters

		mGraphics = new ArrayList<GraphicObject>();
		mMonster = new ArrayList<GraphicObject>();
		mApples = new ArrayList<GraphicObject>();
		
		int antRectHeight = 30;
		int antRectWidth = 30;
		int[] colors2 = new int[antRectWidth * antRectHeight];
		for (int i = 0; i < colors2.length; i++){
			colors2[i] = Color.BLUE;
		}
		/*ImageView antPhoto = (ImageView)findViewById(R.drawable.ant);
		Bitmap antMap = BitmapFactory.decodeFile("/Ant Project/res/drawable-mdpi/ant.png");
		antPhoto.setImageBitmap(antMap);
		
		*/
		Bitmap ant = Bitmap.createBitmap(colors2, antRectWidth, antRectHeight, Bitmap.Config.ARGB_8888);
		mAnt = new GraphicObject(ant);
		mAnt.getCoordinates().setX(0);
		mAnt.getCoordinates().setY(0);

		mGraphics.add(mAnt);

		int rectHeight = 40;
		int rectWidth = 40;
		int[] colors = new int[rectWidth * rectHeight];

		for (int i = 0; i < colors.length; i++){
			colors[i] = Color.RED;
		}

		for(int i = 0; i < numMonsters; i ++){
			
			
			
			
			Bitmap monster1 = Bitmap.createBitmap(colors, rectWidth, rectHeight, Bitmap.Config.ARGB_8888);
			GraphicObject monster = new GraphicObject(monster1);			
			mGraphics.add(monster);
			mMonster.add(monster);

			monster.getCoordinates().setX((int)(Math.random()* getWidth()));
			monster.getCoordinates().setY((int)(Math.random() * getHeight()));


		}
		int appleHeight = 20;
		int appleWidth = 20;
		int[] colors3 = new int[appleHeight * appleWidth];
		for(int i = 0; i < colors3.length; i++){
			colors3[i] = Color.GREEN;			
		}
		
		Bitmap appleMap = Bitmap.createBitmap(colors3, appleWidth, appleHeight, Bitmap.Config.ARGB_8888);
		 
		
		for(int i = 0; i < numApples + 1; i ++){
		
		GraphicObject apples = new GraphicObject(appleMap);
		mGraphics.add(apples);
		mApples.add(apples);
		
		apples.getCoordinates().setX((int) (Math.random() * (getWidth() - 90) - 70));
		apples.getCoordinates().setY((int) (Math.random() * (getHeight() - 90) - 70));
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub

	}
	public void surfaceCreated(SurfaceHolder holder) {

		createNewGame();
		mThread.setRunning(true);
		mThread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		mThread.setRunning(false);
		while (retry) {
			try {
				mThread.join();
				retry = false;
			} catch (InterruptedException e) {

			}
		}
	}

	public void setAntX(int x){
		if(mAnt != null){
			if(x > 3){
				mAnt.getCoordinates().setX(mAnt.getCoordinates().getX() + 3);
			}
			else if(x < -3){
				mAnt.getCoordinates().setX(mAnt.getCoordinates().getX() - 3);
			}
		}
	}

	public void setAntY(int y){
		if(mAnt != null){
			if(y > 2){
				mAnt.getCoordinates().setY(mAnt.getCoordinates().getY() + 3);
			}
			else if(y < -2){
				mAnt.getCoordinates().setY(mAnt.getCoordinates().getY() - 3);	
			}
		}
	}

}
